﻿#region 命名空间

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using WinnerCMS.BLL;
using WinnerCMS.Common;
using System.Text;
using WinnerCMS.Model;
using WinnerCMS.Model.Attribute;

#endregion

namespace WinnerCMS.WebUI.Admin.Window
{
    [Access]
    public partial class GetImage : Page
    {
        private int PicLong = 80;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var config = CMSConfig.Instance();
                string strDir;
                strDir = Server.MapPath(config.UpLoadFolder);

                var str = strDir;
                str = str.Replace(Server.MapPath("~/"), "/");
                str = str.Replace("\\", "/");
                this.ddlPath.Items.Insert(0, new ListItem(str, strDir));


                BindList("");
            }
        }

        private void BindList(string strDir)
        {
            var dt_Files = new DataTable();
            DataRow dr_Files;

            dt_Files.Columns.Add(new DataColumn("FileName", Type.GetType("System.String")));
            dt_Files.Columns.Add(new DataColumn("FullName", Type.GetType("System.String")));
            dt_Files.Columns.Add(new DataColumn("FileSize", Type.GetType("System.String")));
            dt_Files.Columns.Add(new DataColumn("LastWriteTime", Type.GetType("System.DateTime")));
            dt_Files.Columns.Add(new DataColumn("CreationTime", Type.GetType("System.DateTime")));

            var config = CMSConfig.Instance();
            //针对当前目录建立目录引用对象
            if (strDir.Length == 0 && strDir != Server.MapPath(config.UpLoadFolder))
            {
                strDir = Server.MapPath(config.UpLoadFolder);
            }
            var dirInfo = new DirectoryInfo(strDir);
            //循环判断当前目录下的文件和目录
            FileInfo fi;
            this.DropDownList1.Items.Clear();
            this.DropDownList1.Items.Add(new ListItem("---", dirInfo.Name));
            foreach (var fsi in dirInfo.GetFileSystemInfos())
            {
                //如果是文件
                if (fsi is FileInfo)
                {
                    dr_Files = dt_Files.NewRow();
                    fi = fsi as FileInfo;

                    dr_Files["FileName"] = fi.Name;
                    dr_Files["FullName"] = fi.FullName.Replace(Server.MapPath("~/"), "/");
                    dr_Files["FileSize"] = fi.Length;
                    dr_Files["LastWriteTime"] = fi.LastWriteTime;
                    dr_Files["CreationTime"] = fi.CreationTime;
                    dt_Files.Rows.Add(dr_Files);
                }
                if (fsi is DirectoryInfo)
                {
                    var li = new ListItem();
                    li.Text = fsi.Name;
                    li.Value = fsi.FullName;
                    this.DropDownList1.Items.Add(li);
                }
            }

            this.PageNavigation1.RecordCount = dt_Files.Rows.Count;

            var ArrDR = dt_Files.Select("", this.cbSorts.SelectedValue);

            this.DL_List.DataSource = ArrDR;
            this.DL_List.DataBind();
        }

        protected void PageNavigation1_PageChanging(object src, WebControls.PageChangingEventArgs e)
        {
        }

        protected string ToolTip(object FileName, object FileSize, object CreationTime, object LastWriteTime)
        {
            var winner = new StringBuilder();
            winner.AppendLine("文 件 名：" + FileName);
            winner.AppendLine("文件大小：" + FileSize);
            winner.AppendLine("建立时间：" + CreationTime);
            winner.AppendLine("最后修改：" + LastWriteTime);

            return winner.ToString();
        }

        protected void DL_List_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            //if (e.Item.ItemType == ListItemType.Item)
            {
                var img = e.Item.FindControl("Img") as Image;

                this.PicLong = int.Parse(this.cbThumbSize.SelectedValue);
                img.Width = this.PicLong;
                img.Height = this.PicLong;
            }
        }

        protected void ReBind(object sender, EventArgs e)
        {
            BindList(this.ddlPath.SelectedValue);
        }

        protected string PicURL(object InPut)
        {
            var OutPut = "ClickRetrun(\"" + InPut.ToString().Replace("\\", "/") + "\")";
            return OutPut;
        }

        protected void ddlPath_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindList(this.ddlPath.SelectedValue);
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Directory.Exists(this.DropDownList1.SelectedValue))
            {
                var str = this.DropDownList1.SelectedValue;
                str = str.Replace(Server.MapPath("~/"), "/");
                str = str.Replace("\\", "/");
                var li = new ListItem(str, this.DropDownList1.SelectedValue);
                if (this.ddlPath.Items.Contains(li) == false)
                {
                    this.ddlPath.Items.Insert(this.ddlPath.Items.Count - 1, li);
                    this.ddlPath.SelectedValue = this.DropDownList1.SelectedValue;
                }


                BindList(this.DropDownList1.SelectedValue);
            }
        }
    }
}